package com.smile.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@Configuration // 一定不要忽略此注解
@Order(Integer.MIN_VALUE)
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                System.out.println("uri:"+request.getRequestURI());
                Enumeration<String> headerNames = request.getHeaderNames();
                while (headerNames.hasMoreElements()) {
                    String headerName = headerNames.nextElement();
                    String headerValue = request.getHeader(headerName);
                    System.out.println(headerName + ":" + headerValue);
                }

                return true;
            }
        });
    }

//    @Bean
//    public FilterRegistrationBean<CorsFilter> filterRegistrationBean() {
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        // 1.允许任何来源
//        corsConfiguration.addAllowedOriginPattern("*");
//        // 2.允许任何请求头
//        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
//        // 3.允许任何方法
//        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
//        // 4.允许凭证
//        corsConfiguration.setAllowCredentials(true);
//
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", corsConfiguration);
//
//        CorsFilter corsFilter = new CorsFilter(source);
//
//        FilterRegistrationBean<CorsFilter> filterRegistrationBean = new FilterRegistrationBean<>(corsFilter);
//        filterRegistrationBean.setName("myCorsFilter");
//        filterRegistrationBean.setOrder(-101);
//        return filterRegistrationBean;
//    }

//    @Bean
//    fun filterRegistrationBean(): FilterRegistrationBean<*>? {
//        val corsConfiguration = CorsConfiguration()
//        //1.允许任何来源
//        corsConfiguration.addAllowedOriginPattern("*");
//        //2.允许任何请求头
//        corsConfiguration.addAllowedHeader(CorsConfiguration.ALL)
//        //3.允许任何方法
//        corsConfiguration.addAllowedMethod(CorsConfiguration.ALL)
//        //4.允许凭证
//        corsConfiguration.allowCredentials = true
//        val source = UrlBasedCorsConfigurationSource()
//        source.registerCorsConfiguration("/**", corsConfiguration)
//        val corsFilter = CorsFilter(source)
//
//        val filterRegistrationBean = FilterRegistrationBean(corsFilter)
//        filterRegistrationBean.setName("myCorsFilter")
//        filterRegistrationBean.order = -101
//        return filterRegistrationBean
//    }




//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                // 所有接口
//                .allowCredentials(true) // 是否发送 Cookie
//                .allowedOriginPatterns("*") // 支持域
//                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"}) // 支持方法
//                .allowedHeaders("*")
//                .exposedHeaders("*");
//    }
}
